$('.input-daterange').datepicker({
  language: "zh-CN"
});
$(function () {

  /*var verifyBtnInterval = null;
  var verifyBtnTime = 60;
  var firstTimeOpenModal = true;

  var baseUserInterval = null;
  var baseUserTime = 60;
  var baseUserFirtTime = true;

  $(document).on('click', '.ethnic-content-dropdown>.dropdown-menu>a', handleEthnicCheck);
  $(document).on('click', '.sex-content-dropdown>.dropdown-menu>a', handleSexCheck);
  $(document).on('click', '.hosCardType-content-dropdown>.dropdown-menu>a', handleHosCardTypeCheck);
  $(document).on('click', '#save', handleSave);
  $(document).on('click', '#submit', handleSubmit);
  $(document).on('click', '#check-base-user', handleCheckBaseUser);
  $(document).on('click', '#baseUserSubmit', handleBaseUserSubmit);
  $(document).on('click', '#base-user-btn', handleBaseUserBtnClick);

  function handleEthnicCheck(e) {
    $('#ethnic')
      .attr('data-id', $(this).attr('data-id'))
      .children('span')
      .first()
      .text($(this).text());
  }

  function handleSexCheck(e) {
    $('#sex')
      .attr('data-id', $(this).attr('data-id'))
      .children('span')
      .first()
      .text($(this).text());
  }

  function handleHosCardTypeCheck(e) {
    $('#hosCardType')
      .attr('data-id', $(this).attr('data-id'))
      .children('span')
      .first()
      .text($(this).text());
  }

  function handleSave(e) {
    var _this = this;
    var username = $('#name').val();
    var usersex = $('#sex').attr('data-id');
    var useridcard = $('#idcard').val();

    if (!(username && usersex && useridcard)) {
      warning_prompt('必填信息缺失');
      return;
    }
    $('#submitModal').modal('show');
    if (firstTimeOpenModal) {
      firstTimeOpenModal = false;
      handleVerifyBtnClick(function () {
      });
    }
  }

  function handleVerifyBtnClick(cb) {
    if (verifyBtnTime === 60) {
      $('#verify-btn').attr('disabled', true);
      $.post('/api/updateUserCaptcha').then(function () {
        cb && cb();
        verifyBtnInterval = setInterval(function () {
          verifyBtnTime--;
          if (verifyBtnTime === 0) {
            clearInterval(verifyBtnInterval);
            verifyBtnTime = 60;
            $('#verify-btn').attr('disabled', false).text('获取验证码');
          } else {
            $('#verify-btn').text(verifyBtnTime + ' S');
          }
        }, 1000);
      }).catch(function (res) {
        cb && cb();
      });
    }
  }

  function handleSubmit(e) {
    var body = {};
    body.username = $('#name').val();
    body.usersex = $('#sex').attr('data-id');
    body.useridcard = $('#idcard').val();
    body.ethnic = $('#ethnic').attr('data-id');
    body.cardtype = $('#hosCardType').attr('data-id') || '';
    body.cardno = $('#ybk').val();
    body.useraddress = $('#address').val();
    body.livingTime = $('#livingTime').val();
    body.invalidTime = $('#invalidTime').val();
    body.captcha = $('#captcha').val();

    if (!(body.username && body.usersex && body.useridcard)) {
      warning_prompt('必填信息缺失');
      return;
    }

    if (!captcha) {
      warning_prompt('验证码不能为空');
      return;
    }
    $.post('/api/updateUser', body).then(function (res) {
      if (_.get(res, 'code') === 0) {
        success_prompt('保存成功');
        $('#captcha').val('');
        $('#submitModal').modal('hide');
      } else {
        fail_prompt('保存失败');
      }
    });

  }

  function handleCheckBaseUser() {
    var name = $('#name').val();
    var sex = {
      value: $('#sex').attr('data-id'),
      text: $('#sex>span:nth-child(1)').text(),
    };
    var idcard = $('#idcard').val();
    var ethnic = {
      text: $('#ethnic>span:nth-child(1)').text(),
      value: $('#ethnic').attr('data-id'),
    };
    var livingTime = $('#livingTime').val();
    var invalidTime = $('#invalidTime').val();

    if (!(name && sex.value && idcard && ethnic.value)) {
      warning_prompt('数据缺失！');
      return;
    }

    $('#base-user-name').val(name);
    $('#base-user-sex').val(sex.text);
    $('#base-user-id').val(idcard);
    $('#base-user-ethnic').val(ethnic.text);
    $('#base-user-date').val(livingTime + ' 至 ' + invalidTime);

    $('#baseUserModal').modal('show');

    if(baseUserFirtTime) {
      handleBaseUserBtnClick();
    }
  }
  function handleBaseUserBtnClick() {
    if(baseUserTime === 60){
      $('#base-user-btn').attr('disabled', true);
      $.get('/api/baseUserCaptcha?idcard=' + $('#base-user-id').val()).then(function() {
        baseUserInterval = setInterval(function(){
          if(baseUserTime === 0) {
            clearInterval(baseUserInterval);
            $('#base-user-btn').text('获取验证码').attr('disabled', false);
            baseUserTime = 60;
          } else {
            baseUserTime--;
            $('#base-user-btn').text(baseUserTime + 'S');
          }
        }, 1000);
      }).catch(function(){
        $('#base-user-btn').attr('disabled', false);
      });
    }
  }

  function handleBaseUserSubmit() {
    $.post('/api/baseUserUpdate', {
      authidcard: $('#idcard').val(),
      authname: $('#name').val(),
      authsex: $('#sex').attr('data-id'),
      authlivingtime: $('#livingTime').val(), 
      authinvalidtime: $('#invalidTime').val(), 
      authethnic: $('#ethnic').attr('data-id'),
      authcaptcha: $('#base-user-captcha').val(),
    }).then(function(res) {
      success_prompt('提交成功');
      $('#baseUserModal').modal('hide');
    });
  }*/
  var userInfo = new Vue({
    el:"#user_form",
    data:{
      id:"",
      name:"zxc",
      sex:"男",
      idcard:"11111111111111",
      nation:"汉",
      phone:"17329570904",
      address:"广东",
      token:localStorage.getItem("token")
    },
    methods: {
      getUserInfo: function () {
        //alert(localStorage.getItem("token"));
        var that = this;
        axios.get("http://localhost:8080/hospital/api/user/getinfo",
            {headers:{'token':localStorage.getItem('token')}}).then(function (response) {
          if (response.data.code!="0"){
            alert(response.data.msg)
            return;
          }
          console.log(response.data.data.userName);
          console.log(that.name)
          that.id = response.data.data.id
          that.name = response.data.data.userName;
          if (response.data.data.userSex == 1) {
            that.sex = "男";
          } else {
            that.sex = "女";
          }
          that.idcard = response.data.data.userIdentityCard;
          that.nation = response.data.data.userNation;
          that.phone = response.data.data.userPhone;
          localStorage.setItem("pic",response.data.data.userWeChatHead);
        }).catch(function (err) {
          console.log(err);
        });
      },
      saveUserInfo: function () {
        var that = this;
        var userSex = 1;
        if (that.sex == "女") {
          userSex = 2;
        }
        alert(that.name)
        axios.post("../api/user/updateinfo"
            ,{
              userName: that.name,
              userSex: userSex,
              userIdentityCard: that.idcard,
              userNation: that.nation,
              userPhone: that.phone,
              id: that.id
            },{headers:{'token':localStorage.getItem('token')}}).then(function (response) {
          console.log(response)
          if (response.data.code!="0"){
            alert(response.data.msg)
            return;
          }
          that.getUserInfo();
        }).catch(function (err) {
          console.log(err);
        });
      }
    },
    created() {
      this.getUserInfo();
    }
  })
});


